package com.store.app.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.store.app.R;
import com.store.app.bean.CommissionRecordBean;
import com.store.app.utils.DateUtil;

import java.util.List;

/**
 * 佣金流水列表的Adapter
 */
public class CommissionRecordAdapter extends BaseAdapter {

    private Context context;
    private List<CommissionRecordBean> list;

    public CommissionRecordAdapter(Context context, List<CommissionRecordBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_commission_record, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //绑定数据
        CommissionRecordBean bean = list.get(position);

        holder.date.setText(DateUtil.format(bean.getCreated_time()));
        String memberPhone = bean.getMember_phone() == null ? "" : bean.getMember_phone();
        String orderId = bean.getOrder_id() == null ? "" : bean.getOrder_id();
        String text = "会员：" + memberPhone + " 订单[" + orderId + "]";

        String status = bean.getCommision_status();
        if ("normal".equals(status)) {
            text = text + "消费了金额 ¥" + bean.getSale_fee() + "，您获取了消费返佣";
        } else if ("revoke".equals(status)) {
            text = text + "退款金额 ¥" + bean.getSale_fee() + "，您佣金被撤销";
        } else if ("transmatic".equals(status)) {
            text = "佣金转零钱";
        } else {
            Log.d("commissionRecordAdapter", "error commission status.");
        }
        holder.info.setText(text);

        String amount = "¥ " + bean.getAmount();
        holder.amount.setText(amount);

        return convertView;
    }

    public static class ViewHolder {
        public TextView date;
        public TextView info;
        public TextView amount;

        public ViewHolder(View rootView) {
            this.date = (TextView) rootView.findViewById(R.id.tv_item_commission_date);
            this.info = (TextView) rootView.findViewById(R.id.tv_item_commission_info);
            this.amount = (TextView) rootView.findViewById(R.id.tv_item_commission_amount);
        }
    }
}
